package learn.springboot;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.PostConstruct;

public class InitClass implements InitializingBean, CommandLineRunner,ApplicationRunner, ApplicationListener<ContextRefreshedEvent> {

    public InitClass() {
        System.out.println("now is doing construct method");
    }

    //bean指定initMethod方法，bean初始化后执行
    public void initMethod() {
        System.out.println("now is doing initMethod");
    }

    //bean初始化后执行
    @PostConstruct
    public void postConstruct(){
        System.out.println("now is doing @PostConstruct");
    }

    //实现ApplicationRunner，bean初始化后执行
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("now is doing afterPropertiesSet");
    }

    //实现CommandLineRunner，SpringBoot启动后后执行
    @Override
    public void run(String... args) throws Exception {
        System.out.println("now is doing run method of CommandLineRunner Interface");
    }

    //实现CommandLineRunner，SpringBoot启动后后执行
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("now is doing run method of ApplicationRunner Interface");
    }


    //监听SpringBean加载事件，在springboot 初始化完成时执行
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("now is doing something when catch a contextRefreshedEvent");
    }


}
